﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SES.CMS.DO;
using SES.CMS.BL;
using System.Data;
using System.Web.UI.HtmlControls;

namespace SES.CMS
{
    public partial class Category : System.Web.UI.Page
    {
        public string cateHref = "";
        public string cateTitle = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(Request.QueryString["ID"]))
            {
                int categoryID = int.Parse(Request.QueryString["ID"]);
                LoadArticleByCategory(categoryID);
                cmsCategoryDO objCategory = new cmsCategoryBL().Select(new cmsCategoryDO() { CategoryID = categoryID });
                cateHref = "/danh-muc-tin/" + FriendlyUrl(objCategory.Title) + "/" + objCategory.CategoryID;
                cateTitle = objCategory.Title;
                
                //SEO
                if (!string.IsNullOrEmpty(objCategory.MetaTag))
                {
                    HtmlMeta metaKey = new HtmlMeta();
                    metaKey.Name = "keywords";
                    metaKey.Content = objCategory.MetaTag;
                    Page.Header.Controls.Add(metaKey);
                }
                else
                { }
                HtmlMeta metaDesc = new HtmlMeta();
                metaDesc.Name = "description";
                if (!string.IsNullOrEmpty(objCategory.MetaDes))
                {
                    metaDesc.Content = objCategory.MetaDes;
                }
                else
                {
                    metaDesc.Content = objCategory.Description;
                }
                Page.Header.Controls.Add(metaDesc);

                if (!string.IsNullOrEmpty(objCategory.PageTitle))
                {
                    Page.Title = objCategory.PageTitle + " | " + Ultility.WebTitle();
                }
                else
                {
                    Page.Title = objCategory.Title + " | " + Ultility.WebTitle();
                }
            }
            else
            {
                Response.Redirect("/Default.aspx");
            }
        }

        private void LoadArticleByCategory(int categoryID)
        {
            rptNews.DataSource = new cmsArticleBL().SelectByCategoryX(categoryID).DefaultView;
            rptNews.DataBind();
        }
        protected string FriendlyUrl(string strTitle)
        {
            return Ultility.Change_AV(strTitle);
        }
        public string WordCut(string text)
        {
            return Ultility.WordCut(text, 60, new char[] { ' ', '.', ',', ';' }) + "...";
        }
    }
}